####################################
# Causal mechanisms
####################################

rm(list=ls())

library(Hmisc)
library(ggplot2)
library(stargazer)
library(foreign)
library(rdrobust)
library(rdd)
library(readstata13)

################
# Prepare data 
################

# read data
load("~/Dropbox/Gender Chile/08_replication/gender_chile_2020aug28.RData")  
names(d)

# bandwidth
h_bw = rdbwselect(d$permanent_temporary,d$margin,cluster=d$cluster)$bws[1]

######################
# Mechanisms
######################

# Ideology
summary(rdrobust(d$party_ideology_s,d$margin,cluster = d$cluster,all=TRUE,h=h_bw))

(rdplot(y = d$party_ideology_s, x = d$margin,  h= h_bw, nbins = 1000, subset = -h_bw <= d$margin & d$margin <= h_bw, 
        binselect="esmv", kernel="triangular", p=1, y.lim = c(-2, 2), title = "Robust CI: [-0.417,0.215]",
        y.label = "Party Ideology", x.label = "Women candidates' margin of victory"))


# Independent
summary(rdrobust(d$independent_minor_party_s,d$margin,cluster = d$cluster,all=TRUE,h=h_bw))

(rdplot(y = d$independent_minor_party_s, x = d$margin,  h= h_bw, nbins = 1000, subset = -h_bw <= d$margin & d$margin <= h_bw, 
        binselect="esmv", kernel="triangular", p=1, y.lim = c(-2, 2), title = "                                       Robust CI: [-0.487,1.045]",
        y.label = "Independents and minor parties", x.label = "Women candidates' margin of victory"))

